const {
    Keypair,
    Transaction,
    SystemProgram,
    LAMPORTS_PER_SOL,
    TransactionInstruction,
    PublicKey
} = require("@solana/web3.js");

const config = require('./config');
const { user1, user2 } = require('./0_keypair');

const SolChain = require('./solChain');

const run = async () => {

    let fromKeypair = await user1();
    let toKeypair = await user2();

    const transaction = new Transaction();
    transaction.add(
        new TransactionInstruction({
            keys:[],
            programId: new PublicKey(config.simple_hello_world.programID2)

        })
    )

    let solChain = new SolChain(config.devNodeUrl2);
    let balance = await solChain.getBalance(fromKeypair.publicKey);
    console.log('from balance: ', balance);
    let tobalance = await solChain.getBalance(toKeypair.publicKey);
    console.log('to balance: ', tobalance);

    let getLatestBlockhash = await solChain.getLatestBlockhash();
    transaction.recentBlockhash = getLatestBlockhash.blockhash;
    transaction.feePayer = fromKeypair.publicKey;



    transaction.sign(fromKeypair);
    let rawTx = transaction.serialize();


    let ret = await solChain.sendRawTransaction(rawTx);
    console.log('ret: ', ret);
    //5FWjpLv3MLsnbm5FNQpsHQ1DBfgn62zhPr2QgWuvrvdgoHNGzNSF3ABdgdTUDHsw1uyrB7Hk8AtSHKhz6Kbhd78x
    //5PUDoBuxbpWvwGCoeeTNfXwjjfXJ7BWJ7xgEscLwuTT9moqiNYjsoJkWJdTHCF6ZKoZWx7uonk1Xau1RQkXCtjio


}
const get_tx = async ()=> {
    let solChain = new SolChain(config.devNodeUrl2);
    let ret = "5PUDoBuxbpWvwGCoeeTNfXwjjfXJ7BWJ7xgEscLwuTT9moqiNYjsoJkWJdTHCF6ZKoZWx7uonk1Xau1RQkXCtjio"
    let tx = await solChain.getTransaction(ret);

    console.log('tx: ', tx);
    /**
     * {
  blockTime: 1708573714,
  meta: {
    computeUnitsConsumed: 152,
    err: null,
    fee: 5000,
    innerInstructions: [],
    loadedAddresses: { readonly: [], writable: [] },
    logMessages: [
      'Program D6rd477vXo1s3WqAe3mSbAushhBwWBVNe61jWyPLwy1M invoke [1]',
      'Program log: Hello, world!',
      'Program D6rd477vXo1s3WqAe3mSbAushhBwWBVNe61jWyPLwy1M consumed 152 of 200000 compute units',
      'Program D6rd477vXo1s3WqAe3mSbAushhBwWBVNe61jWyPLwy1M success'
    ],
    postBalances: [ 9999990000, 1398960 ],
    postTokenBalances: [],
    preBalances: [ 9999995000, 1398960 ],
    preTokenBalances: [],
    rewards: [],
    status: { Ok: null }
  },
  slot: 280313900,
  transaction: {
    message: Message {
      header: [Object],
      accountKeys: [Array],
      recentBlockhash: '4DukmKVePPeamvmkDR3J8Znyhfan3EHADvkxP5MA8STo',
      instructions: [Array],
      indexToProgramIds: [Map]
    },
    signatures: [
      '5FWjpLv3MLsnbm5FNQpsHQ1DBfgn62zhPr2QgWuvrvdgoHNGzNSF3ABdgdTUDHsw1uyrB7Hk8AtSHKhz6Kbhd78x'
    ]
  },
  version: 'legacy'
}

     * 
     */
}

const getBlock = async () => {
    let slot = 280313900;
    let solChain = new SolChain(config.devNodeUrl2);
    let block = await solChain.getBlock(slot);
    //console.log('block: ', block);
    let transactions = block.transactions;
    for(let tx of  transactions) {
        console.log('tx: ', tx);
    }
}

//run();

get_tx();

//getBlock();